﻿///////////////////////////////////////////////////////////////////////////////
//
//  This source is part of the  "Managed Task Framework.NET Runtime"  (MTF.NET Runtime).
//
//
//  Copyright (c) 2011, Multicore Consulting Limited (UK)
//  All rights reserved.
//  http://www.multicoreconsulting.co.uk
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//
//  -  Redistributions of source code must retain the above copyright
//     notice, this list of conditions and the following disclaimer.
//  -  Redistributions in binary form must reproduce the above copyright
//     notice, this list of conditions and the following disclaimer in
//     the documentation and/or other materials provided with the
//     distribution.
//  -  Neither the name of Multicore Consulting Limited nor the names of
//     its contributors may be used to endorse or promote products
//     derived from this software without specific prior written
//     permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
//  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
//  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
//  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
//  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
//  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
//  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
//  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
//  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MTFOrg.MTF.MTFCore
{
	/// <summary>
	/// Defines a collection of events indexed by unique strings.
	/// </summary>
	public class EventDictionary : Event
	{
		/// <summary>
		/// Dictionary of all contained events.
		/// </summary>
		protected Dictionary<string, Event> m_Events;

		/// <summary>
		/// Adds a new event to the dictionary.
		/// </summary>
		/// <param name="key">Key used to index the event</param>
		/// <param name="newEvent">Event to be added</param>
		public void Add(string key, Event newEvent)
		{
			m_Events.Add(key, newEvent);
		}

		/// <summary>
		/// Retrieves an event with the specified key.
		/// </summary>
		/// <param name="key">Key used to index the event</param>
		/// <returns>Event with the specified key</returns>
		public EventArray this[string key]
		{
			get { return m_Events[key].ToEventArray(); }
		}

		/// <summary>
		/// Gets whether the specified key exists in the dictionary already.
		/// </summary>
		/// <param name="key">Key to check existence of</param>
		/// <returns>true if key exists, otherwise false</returns>
		public bool ContainsKey(string key)
		{
			return m_Events.ContainsKey(key);
		}

        /// <summary>
        /// Returns the event with the specified key.
        /// </summary>
        /// <param name="key">Key of event to retrieve</param>
        /// <returns>Event with specified key</returns>
        public Event GetEvent(string key)
        {
            return m_Events[key];
        }

        /// <summary>
        /// Inserts the event with the specified key.
        /// </summary>
        /// <param name="key">Key of event to insert</param>
        /// <param name="eventToAdd">Event to add</param>
        public void SetEvent(string key, Event eventToAdd)
        {
            m_Events.Add(key, eventToAdd);
        }

		/// <summary>
		/// Returns all the events in the event dictionary.
		/// </summary>
		public Event[] Events
		{
			get
			{
				int i = 0;
				Event[] events = new Event[m_Events.Count];
				foreach (Event e in m_Events.Values)
					events[i++] = e;

				return events;
			}
		}

		/// <summary>
		/// Returns all the keys in the event dictionary.
		/// </summary>
		public string[] Keys
		{
			get
			{
				int i = 0;
				string[] keys = new string[m_Events.Count];
				foreach (string k in m_Events.Keys)
					keys[i++] = k;

				return keys;
			}
		}

		/// <summary>
		/// Default constructor.
		/// </summary>
		public EventDictionary()
		{
			m_Events = new Dictionary<string, Event>();
		}

		/// <summary>
		/// Constructor that adds a single event automatically.
		/// </summary>
		/// <param name="key">Key of single event to add</param>
		/// <param name="newEvent">Single event to add</param>
		public EventDictionary(string key, Event newEvent)
		{
			m_Events = new Dictionary<string, Event>();
			Add(key, newEvent);
		}

        /// <summary>
        /// Converts an event to an event array
        /// </summary>
        /// <returns>Event array containing EventDictionary</returns>
        public EventArray ToEventArray()
        {
            return new EventArray(this);
        }

        /// <summary>
        /// Gets an item from the event dictionary.
        /// </summary>
        /// <param name="key">The string key used to retrieve the item</param>
        /// <param name="index">The integer index of the item</param>
        /// <returns>Item at key and index</returns>
        public object GetItem(string key, int index)
        {
            QueueEvent queueEvent = this[key][index] as QueueEvent;
            return queueEvent.Item;
        }

        /// <summary>
        /// Sets an item in the event dictionary.
        /// </summary>
        /// <param name="key">The string key used to set the item</param>
        /// <param name="index">The integer index of the item</param>
        /// <param name="item">Item to set at key and index</param>
        public void SetItem(string key, int index, object item)
        {
            QueueEvent queueEvent = this[key][index] as QueueEvent;
            queueEvent.Item = item;
        }
    }
}
